package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 *
 * 黑名单策略
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/20 22:30
 * description:
 */
@Service("black")
@Slf4j
public class BlackFilterServiceImpl implements FilterChainService {
    /*
    * 平台实现系统级的手机号黑名单控制，限制某些号码的消息的发送，对发送短信进行过滤
    * 如果是黑名单就把发送日志存到Elasticsearch
    * 如果同时是HTTP的发送方式就模拟出状态报告推送给客户
    * */

    @Autowired
    PushMsgUtil pushMsgUtil;

    @Autowired
    CacheFeign cacheFeign;

    @Override
    public boolean strategy(StandardSubmit submit) {

        log.info("[策略模块 -- 黑名单策略] 策略开始执行！ submit={}",submit);
        //获取手机号
        String mobile = submit.getDestMobile();
        //获取发送方式
        Integer source = submit.getSource();
        String key= CacheConstants.CACHE_PREFIX_BLACK+mobile;
        String s = cacheFeign.get(key);
        if(!StringUtils.isEmpty(s)){
            if(source==1){
                pushMsgUtil.pushReport(submit,"发送失败:该用户已被拉黑"+mobile);
            }
            log.info("[策略模块 -- 黑名单策略] 发送失败！ 黑名单手机号={}",mobile);
            pushMsgUtil.sendLog(submit,"发送失败:该用户已被拉黑"+mobile);
            return false;

        }
        log.info("[策略模块 -- 黑名单策略] 执行完毕！ ");

        return true;
    }
}
